/*	How to use it:
 * 	
 *	copy these lines, to your class and call them insted of "trace()"	=)
 * 
 */
//	//	normal message			balck	white
//	private	function	onTrace(...args):void{NetTrace.send('onTrace',String(Object(this).constructor),args);}
//	//	error message			red		bold
// 	private	function	eTrace(...args):void{NetTrace.send('eTrace',String(Object(this).constructor),args);}
//	//	warning message			orange	bold
//	private	function	wTrace(...args):void{NetTrace.send('wTrace',String(Object(this).constructor),args);}
//	//	highlighted message		white	bold
//	private	function	hTrace(...args):void{NetTrace.send('hTrace',String(Object(this).constructor),args);}
//
//	//	and also 			g-green	b-blue	y-yellow	m-magenta	c-cyan	s-silver	colors can be used	(all bold)
//	//	or one of these: 	gTrace 	bTrace	yTrace		mTrace		cTrace	sTrace
/*
 * 
 * 
 */  
package lib.localTrace {
	import flash.events.AsyncErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.events.StatusEvent;
	import flash.events.TimerEvent;
	import flash.net.LocalConnection;
	import flash.utils.Timer;	

	//	import lib.Alias;	

	/**
	 * @author pettot
	 */
// DEF #####################################################
public class LocalTrace
{
	private	static	const	CONNECTION_ID:String='_RemoteTraceConnectionId_2AnyURL_by_ppy@LBi';
	private	static	var		localConnection:LocalConnection=new LocalConnection();
	private	static	var		reconnecring:Boolean=false;
	private	static	var		isFirst:Boolean=true;
	private	static	var		callerContent:*;
	public	static	var		SIGN:uint=500;
// DEF #####################################################
public	function	set client(this_:*):void{callerContent=this_;connect();}
public	function	LocalTrace(){}
//	it handels everiting with the sending trace() information throung Local Connection
public	static	function	send(CalledMethodName:String,...args):void
{
	var	className:String=args.shift();
	args=String(args).split('\n');
	for(var i:uint=args.length;i--;){sendSL(CalledMethodName,className,String(args.shift()));}
}
//	send a Signle Line, line brakes are removed!
public	static	function	sendSL(CalledMethodName:String,...args):void
//public	function	send(...args):void
{
//	var localConnection:LocalConnection=new LocalConnection();
	localConnection.allowDomain('*','localhost');
	localConnection.allowInsecureDomain('*','localhost');
	localConnection.addEventListener(AsyncErrorEvent.ASYNC_ERROR, onAsyncError);
	localConnection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
	localConnection.addEventListener(StatusEvent.STATUS, onStatus);
	args.length>1?
		localConnection.send(CONNECTION_ID,CalledMethodName,args.shift(),args):
		localConnection.send(CONNECTION_ID,CalledMethodName,args);
	isFirst?runOnce():null;
}
//	it start Listening (connection)
public	function	connect():void
{
	close();
	localConnection=new LocalConnection();
	localConnection.client=callerContent;
	localConnection.allowDomain('*','localhost');
	localConnection.allowInsecureDomain('*','localhost');
	localConnection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
	localConnection.addEventListener(AsyncErrorEvent.ASYNC_ERROR, onAsyncError);
	localConnection.addEventListener(StatusEvent.STATUS, onStatus);
	reconnecring=true;
	connecting();
}
private	function	connecting(e:TimerEvent=null):void
{
	if(!reconnecring && e is TimerEvent)
	{
		Timer(e.target).removeEventListener(TimerEvent.TIMER,connecting);
		Timer(e.target).stop();
		reconnecring=false;
		return;
	}
	callerContent['callback']('localMsg',"    Trying to connect... ");
	try {localConnection.connect(CONNECTION_ID); reconnecring=false; return;}
	catch(ae:ArgumentError)
	{
		callerContent['callback']('localMsg',"    Can't connect...the connection name is already being used by another SWF... ");
	}
	catch(te:TypeError)
	{
		callerContent['callback']('localMsg','TypeError... it should`nt be ... connection error... ');
		callerContent['callback']('localMsg','this line is missing somewhere:"localConnection=new LocalConnection();"'+te);
		callerContent['callback']('localMsg',''+te);
	}
	catch(ee:Error)
	{
		callerContent['callback']('localMsg','	Unknown connection error...\n'+ee);
	}
	if(e==null)
	{
		var	timer:Timer=new	Timer(SIGN);
			timer.addEventListener(TimerEvent.TIMER,connecting);
			timer.start();
	}
}
// Private #####################################################
//	to make a signal for chacking the connection...
private	static	function	runOnce():void
{
	isFirst=false;localConnection.send(CONNECTION_ID, "connected");
	var	timer:Timer=new Timer(SIGN);
		timer.start();
		timer.addEventListener(TimerEvent.TIMER,sendSign);
}
private static	function	sendSign(event:TimerEvent):void{localConnection.send(CONNECTION_ID,'Sign');}
private	function	close():void
{
	try
	{
		localConnection.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
		localConnection.removeEventListener(AsyncErrorEvent.ASYNC_ERROR, onAsyncError);
		localConnection.removeEventListener(StatusEvent.STATUS, onStatus);
		localConnection.close();
		localConnection=undefined;
	}catch(e:Error){}
}
// Events #####################################################
//
private	static	function	onAsyncError(e:AsyncErrorEvent):void
{
	trace('AsyncError		'+e.target+e.text);
	callerContent?callerContent['callback']('Event',e):null;
}
private	static	function	onSecurityError(e:SecurityErrorEvent):void
{
	trace('	SecurityError	'+e.target+e.text);
	callerContent?callerContent['callback']('Event',e):null;
}
private	static	function	onStatus(e:StatusEvent):void
{
	if(e.level=='status'||e.level=='error'){return;}
	trace('	onStatus		'+e.target+'	'+e.type+'	'+e.level);
	callerContent?callerContent['callback']('Event',e):null;
}
// END #####################################################
}	//class
}	//package
// END #####################################################